
window.$= layui.$;
$(function () {
    //首页的协会工作这里只显示第一类的内容
    $($(".aboutus-ul").get(0)).show();

    $(document).on('mouseover','.subnav .item',function() {
        var index = $(this).attr("data-index");
        $(".aboutus-ul").hide();
        $($(".aboutus-ul").get(index)).show();
    });
    $(document).on('click','#btn-close',function() {
        $("#search_form").hide();
    });
    $(document).on('click','.fa-search',function() {
        $("#search_form").show();
    });
    $(document).on('click','.btn-fa-search',function() {
        $("#search_form").show();
    });
    $(".search-list li").click(function () {
        $("input[name=search-type]").val($(this).attr("data-type"));
        $(this).siblings().removeClass("selected");
        $(this).addClass("selected");
    });
    $("#btn-search").click(function () {
        var keyword = $("input[name=search-keyword]").val();
        if(keyword==false){
            layer.open({
                title: '搜索失败',area:"400px"
                ,content: '请输入您要搜索的关键字'
            });
            return false;
        }
        $("form[name=search-form]").submit();
    });

    $(".item-abs").hover(function(){
        $(this).hide();
        $(this).siblings().addClass("active").show();
    });

    $(".item-detail").hover(function(){

    },function(){
        $(this).siblings().show();
        $(this).removeClass("active").hide();
    });

    $(".logout").click(function () {
        var othis = $(this);
        var type = "auto"
            ,text = othis.text();
        layer.open({
            type: 1
            ,title:"确认退出"
            ,offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
            ,id: 'loginDlg2'+type //防止重复弹出
            ,content: '<div style="padding-top:40px;margin:0 auto;font-size:16px;text-align: center;">确定退出登录吗?</div>'
            ,btn: ['确认退出', '取消']
            ,btnAlign: 'c' //按钮居中
            ,shade: 0.4 //不显示遮罩
            ,area: ['360px', '200px']
            ,yes: function(){
                $.post("/login/doOut.html", {}, function() {
                    delCookie("username");
                    delCookie("password");
                    location.reload();
                },"json");
            }
        });
    });

    //弹出登录对话框
    $(".login").click(function () {
        let type = "auto";
        $.post("/login/dialog.html", {}, function(html) {
            layer.open({
                type: 1
                ,title:"登录"
                ,offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                ,id: 'loginDlg'+type //防止重复弹出
                ,content: html
                ,shade: 0.4 //不显示遮罩
                ,area: ['360px', '240px']
                ,yes: function(){
                    layer.closeAll();
                }
            });
        });
    });


    //发送登录请求
    $(document).on('click','button[name=login]',function() {
        var data = {
            "username": $("input[name=username]").val(),
            "password": hex_md5 ($("input[name=password]").val()),
        };
        _postLogin(data);
    });

    autoLogin(); //发起自动登录
    function autoLogin(){
        if(getCookie("username")
                && getCookie("password")
                        && $("input[name=loginStatus]").val()=="0"){
            layer.msg("正在自动登录...");
            let data = {
                "username": getCookie("username"),
                "password": getCookie("password"),
            }
            _postLogin(data);
        }
    }

    function _postLogin(data) {
        $.post("/login/check.html", data, function(res) {
            if(res.code==0){
                layer.msg(res.info);
            }else{
                layer.msg(res.info);
                if($("input[name=auto-login]").prop('checked')){
                    setCookie("username", data.username);
                    setCookie("password", data.password);
                }
                setTimeout(function () {
                    location.href="/";
                }, 3000);
            }
        },"json");
    }
    function delCookie(name) {
        let exp = new Date();
        exp.setTime(exp.getTime() - 1);
        let cval = getCookie(name);
        if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }

    function getCookie(name) {
        let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) return unescape(arr[2]);
        else return null;
    }

    function setCookie(name, value, days=7) {
        let exp = new Date();
        exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }
});
